/*
 * JavaScript String Utils
 */

function startsWith(str, prefix) {
	if (typeof str !== "string" || typeof prefix !== "string") {
		print(str + ", " + prefix + " is not defined properly!");
		return;
	}

	var prefixStr = str.slice(0, prefix.length)
	if (prefixStr === prefix) {
		return true;
	}

	return false;
}

/*
 * JavaScript Math Utils
 */

function absolute(number) {
	if (typeof number !== "number") {
		print(number + " is not a correct number value!");
		return;
	}

	return number >= 0 ? number : -number;
}

function power(base, expo) {
	if (typeof base !== "number" || typeof expo !== "number") {
		print("base: " + base + " , expo:" + expo
				+ " is not in a correct number value!");
		return;
	}

	var result = 1;
	for ( var i = 1; i <= expo; i++) {
		result *= base;
	}
	return result;
}

function range(ran) {
	if (typeof ran !== "number") {
		print(ran + " is not a correct number!");
		return;
	}

	if (ran === 0) {
		return [ 0 ];
	}

	function absolute(number) {
		return number >= 0 ? number : -number;
	}

	function doRange(innerRange, positive) {
		var rangeArr = [];
		for ( var i = 0; i <= absolute(innerRange); i++) {
			if (positive)
				rangeArr[i] = i;
			else
				rangeArr[i] = -i;
		}
		return rangeArr;
	}

	return doRange;
}

/*
 * Encoding and Decoding UTF-8
 */
function encode_utf8(s) {
	return unescape(encodeURIComponent(s));
}

function decode_utf8(s) {
	return decodeURIComponent(escape(s));
}

/*
 * Object Utils
 * 
 */

function forEach(array, action) {
	for (var i = 0; i < array.length; i++) {
		action(i, array[i]);
	}
}


function forEachIn(obj, func) {
	if (typeof obj !== 'object' || typeof func !== 'function') {
		throw {
			error: "Type Error",
			message: "Paremters Type are not correct!!!"
		};
		return;
	}
	
	for (var prop in obj) {
		if (Object.prototype.hasOwnProperty.call(obj, prop) && Object.prototype.propertyIsEnumerable.call(obj, prop)) {
			func(prop, obj[prop]);
		}
	}
}

function Dictionary (initValues) {
	this.values = initValues || {};
}

Dictionary.prototype.store = function (name, value) {
	this.values[name] = value;
}

Dictionary.prototype.lookup = function (name) {
	return this.values[name];
}

Dictionary.prototype.contains = function (name) {
	return Object.prototype.hasOwnProperty.call(this.values, name) && Object.prototype.propertyIsEnumerable.call(this.values, name);
}

Dictionary.prototype.each = function (action) {
	forEachIn(this.values, action);
}











